<?php

define('TEMPLATE_ERROR',0);
define('TEMPLATE_INFO',1);
define('TEMPLATE_SUCCESS',2);

class template {
	
	var $tpl_file; // Riferimento al file del template corrente
	var $tpl; // Il contenuto del template corrente
	var $values; // Un array con le parti del template sostituite dal contenuto effettivo
	var $modules = array(); // Array con i moduli caricati
	var $responses = array(); // Array con tutti i messaggi di risposta
	
	function template($file) {
		$this->set_template($file);
	}
	
	function set_template($file) {
		$this->tpl_file = $file;
		$this->tpl = file_get_contents($this->tpl_file) or trigger_error('Error reading file '. $this->tpl_file, E_USER_ERROR);
	}
	
	function set($name, $value) {
		$this->values[$name] .= $value;
	}
	
	function set_file($name, $cnt_file, $lang = null) {
		if ($lang) {
			$ext = end(explode('.', $cnt_file));
			$fname = substr($cnt_file, 0, -(strlen($ext)+1));
			$filename = $fname ."_". $lang .".". $ext;
			if (is_file($filename)) $cnt_file = $filename;
		}
		$cnt = file_get_contents($cnt_file) or trigger_error('Error reading file '. $cnt_file, E_USER_ERROR);
		//$this->values[$name] .= $cnt;
		
		$this->tpl = str_replace("<!-- ". $name ." -->", $cnt, $this->tpl);
		
	}
	
	function replace($name, $value) {
		$this->values[$name] = $value;
	}
	
	function remove($name) {
		unset($this->values[$name]);
	}
	
	function include_module($name) {
		$dir = '../modules/'.$name;
		if (!$this->module_included($name, $this->modules) && is_dir($dir)) {
			if (is_file( $dir ."/". $name . ".js")) {
				$this->set('includes', "<script type=\"text/javascript\" language=\"Javascript\" src=\"". $dir ."/". $name .".js\"></script>\n");
			}
			if (is_file( $dir ."/". $name . ".css")) {
				$this->set('includes', "<link href=\"". $dir ."/". $name . ".css\" rel=\"stylesheet\" type=\"text/css\" />\n");
			}
			if (is_file( $dir ."/". $name . ".class.php")) {
				include $dir ."/". $name .".class.php";
			}
			$this->modules[] = $name;
		}
	}
	
	function module_included($name) {
		return (in_array($name, $this->modules));
	}
	
	function include_script($file) {
		$dir = '../includes/';
		$this->set('includes', "<script type=\"text/javascript\" language=\"Javascript\" src=\"". $dir.$file ."\"></script>\n");
	}
	
	function include_css($file) {
		$dir = '../includes/';
		$this->set('includes', "<link href=\"". $dir.$file ."\" rel=\"stylesheet\" type=\"text/css\" />\n");
	}
	
	function metatag($value) {
		$this->set('metatag', $value);
	}
	
	function title($value) {
		$this->set('title', $value);
	}
	
	function pagetitle($value) {
		$this->set('pagetitle', $value);
	}
	
	function response($msg, $type = TEMPLATE_SUCCESS, $delayed = false) {
		if ($delayed) {
			$_SESSION['template_delayed_responses'][] = array($type => $msg);
		} else {
			$this->responses[] = array ($type => $msg);			
		}

	}
	
	function clean_responses() {
		$this->responses = null;
	}
	
	function content($lang = '') {
		$file = "_". basename($_SERVER['PHP_SELF'],".php") .".html";
		$this->set_file('content', $file, $lang);
	}
	
	function footer($value) {
		$this->set('footer', $value);
	}
	
	function validate_form() {
		foreach ($this->responses as $type => $v) {
			if ($type == TEMPLATE_ERROR) return false;
		}
		return true;
	}
	
	function nav($cnt, $name = "nav") {
		$navlen = count($cnt);
		$i = 0;
		$nav = "<ul>\n";
		foreach ($cnt as $label => $attributes) {
			if ($i == 0) {
				if (isset($attributes['class'])) {
					if (!stristr($attributes['class'], 'first')) $attributes['class'] .= ' first';
				} else {
					$attributes['class'] = 'first';
				}
			}
			if ($i == ($navlen-1)) {
				if (isset($attributes['class'])) {
					if (!stristr($attributes['class'], 'last')) $attributes['class'] .= ' last';
				} else {
					$attributes['class'] = 'last';
				}
			}
			$nav .= "<li". ($attributes['class'] ? " class=\"". $attributes['class'] ."\"" : "") .">";
			$nav .= "<a";
			foreach ($attributes as $attribute => $value) $nav .= " ". $attribute ."=\"". $value ."\"";
			$nav .= ">";
			if ($attributes['accesskey'])	$label = preg_replace('/('. $attributes['accesskey'] .')/i', '<u>$1</u>', $label, 1);
			$nav .= $label ."</a></li>\n";
			$i++;
		}
		$nav .= "</ul>\n";
		$this->set($name, $nav);
	}
	
	function subnav($cnt, $show_subdir_if_in_dir = true, $name = "nav") {
		$nav = $this->_subnav($cnt, $show_subdir_if_in_dir);
		$this->set($name, $nav);
	}
	
	function _subnav ($cnt, $show_subdir_if_in_dir) {
		$navlen = count($cnt);
		$i = 0;
		$ret = "<ul>\n";
		foreach ($cnt as $item) {
			if ($i == 0) {
				if (isset($item['class'])) {
					if (!stristr($item['class'], 'first')) $item['class'] .= ' first';
				} else {
					$item['class'] = 'first';
				}
			}
			if ($i == ($navlen-1)) {
				if (isset($item['class'])) {
					if (!stristr($item['class'], 'last')) $item['class'] .= ' last';
				} else {
					$item['class'] = 'last';
				}
			}
			
			list(,$href_dir,$href_file) = explode('/', $item['href']);
			if ($href_dir == this_dir() && ($href_file == this_page() || ($href_file == '' && this_page() == 'index.php'))) {
				$li_class = 'actual';
			} else {
				$li_class = '';
			}
			
			$ret .= "<li". ($item['class'] ? " class=\"". $item['class'] . ($li_class ? " ". $li_class : "") ."\"" : ($li_class ? " class=\"". $li_class ."\"" : "")) ."><a";
			foreach ($item as $attribute => $value) {
				if (substr($attribute, 0, 1) != '_') $ret .= " ". $attribute ."=\"". $value ."\"";
			}
			$ret .= ">";
			if ($item['accesskey'])	$item['_label'] = preg_replace('/('. $item['accesskey'] .')/i', '<u>$1</u>', $item['_label'], 1);
			
			$ret .= $item['_label']."</a>";
			
			if (is_array($item['_submenu']) &&
			    (($show_subdir_if_in_dir && this_dir() == basename($item['href']) || !$show_subdir_if_in_dir)) ) {
				$ret .= $this->_subnav($item['_submenu'], $show_subdir_if_in_dir);
			}
			$ret .= "</li>\n";
			$i++;
		}
		$ret .= "</ul>\n";
		
		return $ret;
	}
	
	function out() {
		
		if (is_array($_SESSION['template_delayed_responses'])) {
			$this->responses = array_merge($_SESSION['template_delayed_responses'], $this->responses);
			unset($_SESSION['template_delayed_responses']);
		}
		
		if (is_array($this->responses)) {
			foreach($this->responses as $response) {
				foreach ($response as $type => $msg) {
					switch ($type) {
						case TEMPLATE_ERROR   : $errors    .= "\t\t\t<li>". $msg ."</li>\n"; break;
						case TEMPLATE_INFO    : $infos     .= "\t\t\t<li>". $msg ."</li>\n"; break;
						case TEMPLATE_SUCCESS : $successes .= "\t\t\t<li>". $msg ."</li>\n"; break;
					}
				}
			}
		}
		
		if ($errors || $infos || $successes) $this->set('response', "<div id=\"response\">\n");
		
		if ($errors)    $this->set('response', "\t<div class=\"error\">\n\t<ul>\n".   $errors    ."\t</ul>\n\t</div>\n");
		if ($infos)     $this->set('response', "\t<div class=\"info\">\n\t<ul>\n".    $infos     ."\t</ul>\n\t</div>\n");
		if ($successes) $this->set('response', "\t<div class=\"success\">\n\t<ul>\n". $successes ."\t</ul>\n\t</div>\n");
		
		if ($errors || $infos || $successes) $this->set('response', "</div>\n");
		
		$tpl = $this->tpl;
				
		if (is_array($this->values)) {
			foreach ($this->values as $k => $v) {
				$tpl = str_replace("<!-- ". $k ." -->", $v, $tpl);
			}
		}
		if (!DEBUG) $tpl = preg_replace("/<!-- (.*) -->/mU", "", $tpl);
		return $tpl;
	}
}

?>